﻿using BCCommon;
using BCCommon.Enums;
using BCData.Trading.SaleContractChecklist;
using BCDto.Trading.SaleContractChecklist;
using BCEntity.Trading.SaleContractChecklist;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCService.Trading.SaleShoppingCart;
using BCService.Trading.SaleGoods.SaleGoods;

namespace BCService.Trading.SaleContractChecklist
{
    public class SaleContractChecklistService : ISaleContractChecklistService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IApplicationContextService applicationContextService;
        private readonly IRedisService redisService;
        private readonly ISaleContractChecklistData saleContractChecklistData;
        private readonly ISaleShoppingCartService saleShoppingCartService;
        private readonly ISaleGoodsService saleGoodsService;

        private readonly string key = string.Format("{0}", EntityNameConst.SaleContractChecklistEntity);

        public SaleContractChecklistService(IDatabaseContext databaseContext,
            IApplicationContextService applicationContextService,
            IRedisService redisService,
            ISaleContractChecklistData saleContractChecklistData,
            ISaleShoppingCartService saleShoppingCartService,
            ISaleGoodsService saleGoodsService)
        {
            this.databaseContext = databaseContext;
            this.applicationContextService = applicationContextService;
            this.redisService = redisService;
            this.saleContractChecklistData = saleContractChecklistData;
            this.saleShoppingCartService = saleShoppingCartService;
            this.saleGoodsService = saleGoodsService;
        }

        #region 检查销售合同清单是否存在
        public bool Exists(long saleContractChecklistId)
        {
            return this.saleContractChecklistData.Exists(saleContractChecklistId).GetAwaiter().GetResult();
        }
        public bool Exists(long saleContractId, long saleGoodsId)
        {
            return this.saleContractChecklistData.Exists(saleContractId, saleGoodsId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加销售合同清单
        public SaleContractChecklistDto Add(SaleContractChecklistRequestDto requestDto)
        {
            if (!this.saleShoppingCartService.Exists(requestDto.SaleShoppingCartId))
            {
                throw new ArgumentException("购物车商品不存在!");
            }

            var shoppingCartDto = this.saleShoppingCartService.Get(requestDto.SaleShoppingCartId);
            if (!shoppingCartDto.SaleAmount.HasValue)
            {
                throw new ArgumentException(string.Format("此商品处于议价状态，无法创建订单!商品名称：{0}",shoppingCartDto.GoodsName));
            }
            if (!this.saleGoodsService.Exists(shoppingCartDto.SaleGoodsId))
            {
                throw new ArgumentException("销售商品信息不存在!");
            }

            if (!this.saleGoodsService.GoodsNormExists(shoppingCartDto.SaleGoodsNormId))
            {
                throw new ArgumentException("销售商品款式信息不存在!");
            }
            var saleGoodsDto = this.saleGoodsService.Get(shoppingCartDto.SaleGoodsId);
            if (!saleGoodsDto.Enabled)
            {
                throw new ArgumentException("此销售商品已下架!");
            }

            var saleGoodsNormDto = this.saleGoodsService.GetGoodsNorm(shoppingCartDto.SaleGoodsNormId);
            if (!saleGoodsNormDto.Enabled)
            {
                throw new ArgumentException("此销售商品款式已下架!");
            }

            var entity = requestDto.As<SaleContractChecklistEntity>();
            entity.SaleGoodsId = saleGoodsDto.SaleGoodsId;
            entity.SaleGoodsCategoryId = saleGoodsDto.SaleGoodsCategoryId;
            entity.CategoryName = saleGoodsDto.CategoryName;
            entity.CategoryNamePinyin = saleGoodsDto.CategoryNamePinyin;
            entity.GoodsName = saleGoodsDto.GoodsName;
            entity.GoodsNamePinyin = saleGoodsDto.GoodsNamePinyin;
            entity.Sku = saleGoodsDto.Sku;
            entity.SaleGoodsNormId = saleGoodsNormDto.SaleGoodsNormId;
            entity.NormName = saleGoodsNormDto.NormName;
            entity.NormNamePinyin = saleGoodsNormDto.NormNamePinyin;
            entity.NormNo = saleGoodsNormDto.NormNo;
            entity.MainImage = saleGoodsNormDto.MainImage;
            entity.SaleAmount = shoppingCartDto.SaleAmount.Value;
            entity.SaleUnit = shoppingCartDto.SaleUnit;
            entity.SaleCount = shoppingCartDto.SaleCount;
            return this.saleContractChecklistData.Add(entity).GetAwaiter().GetResult().As<SaleContractChecklistDto>();
        }
        #endregion

        #region 修改销售合同清单
        public SaleContractChecklistDto Update(SaleContractChecklistPutDto putDto)
        {
            if (!this.Exists(putDto.SaleContractChecklistId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<SaleContractChecklistEntity>();
            var result = this.saleContractChecklistData.Update(entity).GetAwaiter().GetResult().As<SaleContractChecklistDto>();
            string cacheKey = this.redisService.GetKey(key, entity.SaleContractChecklistId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除销售合同清单
        public bool Delete(long saleContractChecklistId)
        {
            if (!this.Exists(saleContractChecklistId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.saleContractChecklistData.Delete(saleContractChecklistId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, saleContractChecklistId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取销售合同清单
        public SaleContractChecklistDto Get(long saleContractChecklistId)
        {
            string cacheKey = this.redisService.GetKey(key, saleContractChecklistId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.saleContractChecklistData.GetEntity(saleContractChecklistId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<SaleContractChecklistDto>();
        }
        #endregion

        #region 获取销售合同清单列表
        public IEnumerable<SaleContractChecklistDto> GetList(long saleContractId)
        {
            string cacheKey = this.redisService.GetKey(key, saleContractId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.saleContractChecklistData.GetList(saleContractId).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<SaleContractChecklistDto>>();
        }
        #endregion

        #region 获取销售合同清单分页
        public PageableList<SaleContractChecklistDto> Query(long? saleContractId,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, saleContractId, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.saleContractChecklistData.Query(saleContractId,start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<SaleContractChecklistDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<SaleContractChecklistDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}